package com.stn.zjx.givingtreasure.service;

import android.content.Context;

import com.stn.zjx.givingtreasure.database.GreenDaoManager;
import com.stn.zjx.givingtreasure.greendao.UserDao;
import com.stn.zjx.givingtreasure.greendao.WalletUserDao;
import com.stn.zjx.givingtreasure.table.User;
import com.stn.zjx.givingtreasure.table.WalletUser;

import java.util.List;

/**
 * 钱包操作
 */
public class WalletUserService {
    public Context context;
    private WalletUserService(){

    }
    public static final class UserServiceHolder{
        private final static WalletUserService instance=new WalletUserService();
    }
    //单例实例化
    public static WalletUserService getInstance(Context context){
        return UserServiceHolder.instance;
    }


    //添加用户钱包

  public boolean save(WalletUser walletUser){
             if(walletUser==null){
                 return false;
             }
             try {
                 GreenDaoManager.getInstance()
                         .getDaoSession()
                         .getWalletUserDao()
                         .insert(walletUser);
                 return true;
             }catch (Exception e){
                 e.printStackTrace();
             return false;
             }

  }

  //获取在线用户对象
  public WalletUser getWalletUser(String address){
        try {
            WalletUser walletUser = GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getWalletUserDao()
                    .queryBuilder()
                    .where(WalletUserDao.Properties.Address.eq(address)).orderDesc(WalletUserDao.Properties.Id)
                    .unique();
            if(walletUser==null){
                return null;
            }

            return walletUser;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
  }


    //删除钱包用户
    public boolean deleteWalletUser(WalletUser walletUser){
        try {
            GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getWalletUserDao()
                    .delete(walletUser);
          return true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }
    //获取当前所有的钱包
    public List<WalletUser> getAllWalletUser(Integer walletType,Integer type){
        try {
            List<WalletUser> walletUserList = GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getWalletUserDao()
                    .queryBuilder()
                    .where(WalletUserDao.Properties.Wallet_type.eq(walletType))
                    .where(WalletUserDao.Properties.Type.eq(type))
                    .orderDesc(WalletUserDao.Properties.Id)
                    .list();
            if(walletUserList==null||walletUserList.size()==0){
                return null;
            }

            return walletUserList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //获取地址个数
    public Integer getAddressCount(){
        try {
            Integer count = GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getWalletUserDao()
                    .loadAll().size();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

   //用户更新个人信息，资料
    public WalletUser updateWalletUser(WalletUser updateUser){
        WalletUser db_user= GreenDaoManager.getInstance()
                .getDaoSession()
                .getWalletUserDao().queryBuilder()
                .where(WalletUserDao.Properties.Address.eq(updateUser.getAddress()))
                .unique();
        if(db_user!=null){
            updateUser.setId(db_user.getId());
            GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getWalletUserDao()
                    .update(updateUser);
           return updateUser;
        }else{
           return null;
        }
    }
}
